package qq.android.wan.qq;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemLvHistoryAdapter extends BaseAdapter {

    private ArrayList<FriendBean> objects;

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvHistoryAdapter(Context context, ArrayList<FriendBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public FriendBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_lv_history, null);
            viewHolder.tv_lastMessage = (TextView) convertView.findViewById(R.id.tv_lastMessage);
           viewHolder.tv_lastTime = (TextView) convertView.findViewById(R.id.tv_lastTime);
            viewHolder.tv_nick = (TextView) convertView.findViewById(R.id.tv_nick);
            viewHolder.iv_head = (ImageView) convertView.findViewById(R.id.iv_head);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
       viewHolder.tv_nick.setText("这是昵称");
        viewHolder.iv_head.setImageResource(R.drawable.ziji);
       viewHolder.tv_lastTime.setText(shoeTime(objects.get(position).getLastTime()));
        viewHolder.tv_lastMessage.setText(objects.get(position).getLastMessage());
        return convertView;
    }


    protected class ViewHolder {
        private TextView tv_lastMessage, tv_lastTime, tv_nick;
        private ImageView iv_head;
    }

    private String shoeTime(String str_time) {
        String result = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date nowDate = simpleDateFormat.parse(str_time);
            Calendar chatCalendar = Calendar.getInstance();
            chatCalendar.setTime(nowDate);
            Calendar nowCalendar=Calendar.getInstance();
            //1）首先判断是今天
            if(nowCalendar.get(Calendar.YEAR)==chatCalendar.get(Calendar.YEAR)&&nowCalendar.get(Calendar.DAY_OF_YEAR)==chatCalendar.get(Calendar.DAY_OF_YEAR)){
                //显示的是上午和下午
                if(chatCalendar.get(Calendar.HOUR_OF_DAY)>12){
                    //下午
                    result="下午"+(chatCalendar.get(Calendar.HOUR_OF_DAY)-12)+":"+chatCalendar.get(Calendar.MINUTE);
                }else{
                    result="上午"+(chatCalendar.get(Calendar.HOUR_OF_DAY))+":"+chatCalendar.get(Calendar.MINUTE);
                }
            }else{
               // 2）判断是不是昨天
                if(chatCalendar.get(Calendar.YEAR)==nowCalendar.get(Calendar.YEAR)&&chatCalendar.get(Calendar.DAY_OF_YEAR)+1==nowCalendar.get(Calendar.DAY_OF_YEAR)){
                    result="昨天";
                }else {
                    result=dayToWeek(chatCalendar.get(Calendar.DAY_OF_WEEK));
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
    private String dayToWeek(int a){
        String result="星期";
        String num="";
        switch (a){
            case 0: num="日"; break;
            case 1: num="一"; break;
            case 2: num="二"; break;
            case 3: num="三"; break;
            case 4: num="四"; break;
            case 5: num="五"; break;
            case 6: num="六"; break;


        }
        return result+num;
    }
}
